<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="450px" :close-on-click-modal="false">
			<el-form ref="userDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="头像">
							<el-upload
								style="line-height: 1;"
								:show-file-list="false"
								:auto-upload="false"
								accept= "image/jpeg, image/png, image/jpg"
								:on-change="onChange">
								<img v-if="state.ruleForm.picture" :src="state.VITE_IMG_URL + state.ruleForm.picture" class="my-avatar" />
								<div v-else class="my-upload-avatar">
									<SvgIcon name="ele-Plus" :size="20"/>
								</div>
							</el-upload>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="用户账号">
							<el-input v-model="state.ruleForm.user_name" :disabled="state.dialog.type == 'edit'" placeholder="请输入用户账号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="用户邮箱">
							<el-input v-model="state.ruleForm.email" placeholder="请输入用户邮箱" clearable autocomplete="new-password"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="用户昵称">
							<el-input v-model="state.ruleForm.nickname" placeholder="请输入用户昵称" clearable autocomplete="new-password"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="账户密码">
							<el-input v-model="state.ruleForm.password" placeholder="请输入账户密码" type="password" clearable autocomplete="new-password"></el-input>
						</el-form-item>
					</el-col>
					
					<el-col :xs="24" class="mb20">
						<el-form-item label="所属公司">
							<el-select v-model="state.ruleForm.company_ids" filterable placeholder="请选择所属公司" clearable size="default" style="width: 100%;">
								<el-option v-for="(item,index) in state.platsData.company" :key="item.id" :label="item.title" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="套餐">
							<el-select v-model="state.ruleForm.active"  placeholder="请选择套餐" clearable size="default" style="width: 100%;">
								<el-option v-for="(item,index) in state.combos" :key="item.id" :label="item.title" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="关联角色">
							<el-select v-model="state.ruleForm.role_ids" placeholder="请选择关联角色" clearable size="default" style="width: 100%;">
								<el-option v-for="(item,index) in state.roles" :key="item.id" :label="item.cname" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="用户状态">
							<el-switch v-model="state.ruleForm.status" inline-prompt :active-value="1" :inactive-value="0" active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>

					
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
		<CropperDialog ref="cropperDialogRef" @refresh_img="refresh_img"/>
	</div>
</template>

<script setup name="systemUserDialog">
import { reactive, ref, defineAsyncComponent } from 'vue';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 引入组件
const CropperDialog = defineAsyncComponent(() => import('./cropper.vue'));
// 定义变量内容
const cropperDialogRef = ref();
// 定义变量内容
const userDialogFormRef = ref();
import { commApi } from '/@/api/comm/index';
import { memberApi } from '/@/api/member/index';
const state = reactive({
	VITE_IMG_URL:import.meta.env.VITE_IMG_URL,
	ruleForm: {
		user_name: '',
		email: '',
		password: '',
		status: 1,
		company_ids: '',
		role_ids: '',
		nickname: '',
		picture: '',
		active:'',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	platsData:{
		company:[],
		palt:[],
		paltform:[]
	},
	roles:[{
		id:1,
		cname:'管理员'
	},{
		id:2,
		cname:'普通用户'
	}],
	combos:[],
});
const refresh_img = (url) => {
	state.ruleForm.picture = url;
}
/**
 * 文件选择改变时触发
 */
const onChange = (file, fileListVal) => {
	if (file.status !== "ready") return;
	onCropperDialogOpen(file.raw)
}
// 打开裁剪弹窗
const onCropperDialogOpen = (file) => {
	cropperDialogRef.value.openDialog(file);
};
// 打开弹窗
const openDialog = (type, row) => {
	state.dialog.type = type;
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		console.log(state.ruleForm);
		state.dialog.title = '修改会员';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增会员';
		state.dialog.submitTxt = '新 增';
		state.ruleForm = {
			user_name: '',
			email: '',
			password: '',
			status: 1,
			company_ids: '',
			role_ids: '',
			nickname: '',
			picture: '',
			active:''
		}
	}
	state.dialog.isShowDialog = true;
	getcombo()
};
const getcombo = async() =>{
	try {
		const res = await commApi().getCombo();
		if(res.data){
			state.combos = [{
				id:0,
				title:'无套餐'
			},...res.data]
		}
	} catch (error) {
		
	}
	
}
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	console.log(state.ruleForm);
	if (state.dialog.type === 'add') { 
		memberApi().add(state.ruleForm).then(res=>{
			closeDialog();
			emit('refresh');
		})
	}else{
		let formdata = JSON.parse(JSON.stringify(state.ruleForm));
		delete formdata.company_name
		memberApi().edit(formdata).then(res=>{
			closeDialog();
			emit('refresh');
		})
	}
};

// 暴露变量
defineExpose({
	openDialog,
	state
});
</script>
